package com.casic.ops.entity.vo;

import com.casic.ops.entity.domain.CommandExecDO;
import com.casic.ops.utils.Utils;
import com.orion.utils.convert.TypeStore;
import lombok.Data;

/**
 * @ClassName ops-platform
 * @Description  命令执行状态
 * @Author mianbin
 * @Date 2022/5/31 10:59
 * @Version 1.0
 **/
@Data
public class CommandExecStatusVO {

    /**
     * id
     */
    private Long id;

    /**
     * exitCode
     */
    private Integer exitCode;

    /**
     * 状态
     */
    private Integer status;

    /**
     * 使用时间 ms
     */
    private Long used;

    /**
     * 使用时间
     */
    private String keepTime;

    public static CommandExecStatusVO commandExecDO2commandExecStatusVO(CommandExecDO p) {
        CommandExecStatusVO vo = new CommandExecStatusVO();
        vo.setId(p.getId());
        vo.setExitCode(p.getExitCode());
        vo.setStatus(p.getExecStatus());
        if (p.getStartDate() != null && p.getEndDate() != null) {
            vo.setUsed(p.getEndDate().getTime() - p.getStartDate().getTime());
            vo.setKeepTime(Utils.interval(vo.getUsed()));
        }
        return vo;
    }

    static {
        TypeStore.STORE.register(CommandExecDO.class, CommandExecStatusVO.class, p -> {
            CommandExecStatusVO vo = new CommandExecStatusVO();
            vo.setId(p.getId());
            vo.setExitCode(p.getExitCode());
            vo.setStatus(p.getExecStatus());
            if (p.getStartDate() != null && p.getEndDate() != null) {
                vo.setUsed(p.getEndDate().getTime() - p.getStartDate().getTime());
                vo.setKeepTime(Utils.interval(vo.getUsed()));
            }
            return vo;
        });
    }
}
